﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockDataTransferObject;
using System.Globalization;

namespace Presentation.UserControl
{
    public partial class EditProfile : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            object queryRole = Request.QueryString["rname"];
            if (queryRole != null)
            {
                string rolename = queryRole.ToString().ToLower(CultureInfo.CurrentCulture);
                if (rolename == "contractor")
                {
                    ContractorFirstNameRequire.Visible = true;
                    ContractorLastNameRequire.Visible = true;
                    ContractorBirthDateRequire.Visible = true;
                    ContractorBirthDateRegular.Visible = true;
                    ContractorTelephone1Regular.Visible = true;
                    ContractorTelephone2Regular.Visible = true;
                }
                else if (rolename == "client")
                {
                    ClientFirstNameRequire.Visible = true;
                    ClientLastNameRequire.Visible = true;
                    ClientBirthDateRequire.Visible = true;
                    ClientBirthDateRegular.Visible = true;
                }
                else if (rolename == "consultant")
                {
                    ConsultantFirstNameRequire.Visible = true;
                    ConsultantLastNameRequire.Visible = true;
                    ConsultantBirthDateRequire.Visible = true;
                    ConsultantBirthDateRegular.Visible = true;
                }
            }
        }

        public void Binding(AbstractEntity entity)
        {
            if (entity != null)
            {
                User user = entity as User;
                Client client = entity as Client;
                Consultant consultant = entity as Consultant;
                Contractor contractor = entity as Contractor;
                if (user != null)
                {
                    UserNameLabel.Text = user.UserName;
                    RoleLabel.Text = user.Role.RoleName;
                    EmailTextBox.Text = user.Email;
                    if (user.IsActive)
                    {
                        ActiveLabel.Text = "Actived";
                    }
                    else
                    {
                        ActiveLabel.Text = "Blocked";
                    }
                }
                else if (contractor != null)
                {
                    UserNameLabel.Text = contractor.User.UserName;
                    RoleLabel.Text = contractor.User.Role.RoleName;
                    EmailTextBox.Text = contractor.User.Email;
                    if (contractor.User.IsActive)
                    {
                        ActiveLabel.Text = "Actived";
                    }
                    else
                    {
                        ActiveLabel.Text = "Blocked";
                    }
                    ContractorFirstNameTextBox.Text = contractor.FirstName;
                    ContractorLastNameTextBox.Text = contractor.LastName;
                    ContractorBirthDateTextBox.Text = contractor.BirthDate.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture);
                    if (contractor.Gender)
                    {
                        ContractorGenderRadioList.SelectedIndex = 0;
                    }
                    else
                    {
                        ContractorGenderRadioList.SelectedIndex = 1;
                    }
                    ContractorTelephone1TextBox.Text = contractor.Telephone1;
                    ContractorTelephone2TextBox.Text = contractor.Telephone2;
                    ContractorSortCodeTextBox.Text = contractor.SortCode;
                    ContractorBranchTextBox.Text = contractor.Branch;
                    ContractorEmailNotifyDropDownList.SelectedValue = contractor.DayInWeek.ToString(CultureInfo.CurrentCulture);
                }
                else if (client != null)
                {
                    UserNameLabel.Text = client.User.UserName;
                    RoleLabel.Text = client.User.Role.RoleName;
                    EmailTextBox.Text = client.User.Email;
                    if (client.User.IsActive)
                    {
                        ActiveLabel.Text = "Actived";
                    }
                    else
                    {
                        ActiveLabel.Text = "Blocked";
                    }
                    ClientFirstNameTextBox.Text = client.FirstName;
                    ClientLastNameTextBox.Text = client.LastName;
                    ClientBirthDateTextBox.Text = client.BirthDate.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture);
                    if (client.Gender)
                    {
                        ClientGenderRadioList.SelectedIndex = 0;
                    }
                    else
                    {
                        ClientGenderRadioList.SelectedIndex = 1;
                    }
                    ClientAddressTextBox.Text = client.Address;
                    ClientTitleTextBox.Text = client.Title;
                    ClientVatNumberTextBox.Text = client.VatNumber;
                    ClientRegisterNumberTextBox.Text = client.RegisterNumber;
                    ClientEmailNotifyDropDownList.SelectedValue = client.DayInWeek.ToString(CultureInfo.CurrentCulture);
                    ClientPostcodeTextBox.Text = client.Postcode;
                }
                else if (consultant != null)
                {
                    UserNameLabel.Text = consultant.User.UserName;
                    RoleLabel.Text = consultant.User.Role.RoleName;
                    EmailTextBox.Text = consultant.User.Email;
                    if (consultant.User.IsActive)
                    {
                        ActiveLabel.Text = "Actived";
                    }
                    else
                    {
                        ActiveLabel.Text = "Blocked";
                    }
                    ConsultantFirstNameTextBox.Text = consultant.FirstName;
                    ConsultantLastNameTextBox.Text = consultant.LastName;
                    ConsultantBirthDateTextBox.Text = consultant.BirthDate.ToString("MM/dd/yyyy", CultureInfo.CurrentCulture);
                    if (consultant.Gender)
                    {
                        ConsultantGenderRadioList.SelectedIndex = 0;
                    }
                    else
                    {
                        ConsultantGenderRadioList.SelectedIndex = 1;
                    }
                    ConsultantAddressTextBox.Text = consultant.Address;
                }
            }
        }

        public void Filling(AbstractEntity entity)
        {
            User user = entity as User;
            Client client = entity as Client;
            Consultant consultant = entity as Consultant;
            Contractor contractor = entity as Contractor;
            if (entity != null)
            {
                if (user != null)
                {
                    user.Email = EmailTextBox.Text;
                }
                else if (contractor != null)
                {
                    contractor.User.Email = EmailTextBox.Text;
                    contractor.FirstName = ContractorFirstNameTextBox.Text;
                    contractor.LastName = ContractorLastNameTextBox.Text;
                    contractor.BirthDate = Convert.ToDateTime(ContractorBirthDateTextBox.Text, CultureInfo.CurrentCulture);
                    if (ContractorGenderRadioList.SelectedIndex == 0)
                    {
                        contractor.Gender = true;
                    }
                    else
                    {
                        contractor.Gender = false;
                    }
                    contractor.Telephone1 = ContractorTelephone1TextBox.Text;
                    contractor.Telephone2 = ContractorTelephone2TextBox.Text;
                    contractor.SortCode = ContractorSortCodeTextBox.Text;
                    contractor.Branch = ContractorBranchTextBox.Text;
                    contractor.DayInWeek = Convert.ToInt32(ContractorEmailNotifyDropDownList.SelectedValue, CultureInfo.CurrentCulture);
                }
                else if (client != null)
                {
                    client.User.Email = EmailTextBox.Text;
                    client.FirstName = ClientFirstNameTextBox.Text;
                    client.LastName = ClientLastNameTextBox.Text;
                    client.BirthDate = Convert.ToDateTime(ClientBirthDateTextBox.Text, CultureInfo.CurrentCulture);
                    if (ClientGenderRadioList.SelectedIndex == 0)
                    {
                        client.Gender = true;
                    }
                    else
                    {
                        client.Gender = false;
                    }
                    client.Address = ClientAddressTextBox.Text;
                    client.Title = ClientTitleTextBox.Text;
                    client.VatNumber = ClientVatNumberTextBox.Text;
                    client.RegisterNumber = ClientRegisterNumberTextBox.Text;
                    client.DayInWeek = Convert.ToInt32(ClientEmailNotifyDropDownList.SelectedValue, CultureInfo.CurrentCulture);
                    client.Postcode = ClientPostcodeTextBox.Text;
                }
                else if (consultant != null)
                {
                    consultant.User.Email = EmailTextBox.Text;
                    consultant.FirstName = ConsultantFirstNameTextBox.Text;
                    consultant.LastName = ConsultantLastNameTextBox.Text;
                    consultant.BirthDate = Convert.ToDateTime(ConsultantBirthDateTextBox.Text, CultureInfo.CurrentCulture);
                    if (ConsultantGenderRadioList.SelectedIndex == 0)
                    {
                        consultant.Gender = true;
                    }
                    else
                    {
                        consultant.Gender = false;
                    }
                    consultant.Address = ConsultantAddressTextBox.Text;
                }
            }
        }
    }
}